from fastapi import APIRouter, Query,UploadFile
from tortoise.expressions import Q

from app.controllers.system.user import user_controller
from app.settings.config import settings
from app.controllers.customers.customers_list  import customers_controller
from app.schemas.base import Success, SuccessExtra,Fail
from app.schemas.customers import CustomersCreate, CustomersUpdate
from app.utils.upload import upload_image
from app.core.ctx import CTX_USER_ID

customers_list_router = APIRouter()
model_name = "客户"


@customers_list_router.get("/list", summary=f"查看{model_name}列表")
async def do_list(
        page: int = Query(1, description="页码"),
        page_size: int = Query(10, description="每页数量"),
        phone: str = Query('', description="法人电话"),
        name: str = Query('', description="法人姓名"),
        qymc: str = Query('', description="企业名称"),
        status: int = Query(-1, description="状态"),
):
    admin_id = CTX_USER_ID.get()
    super_admin = await user_controller.is_super_admin(user_id=admin_id)

    q = Q()
    if phone:
        q &= Q(phone__contains=phone)
    if name:
        q &= Q(name__contains=name)
    if qymc:
        q &= Q(qymc__contains=qymc)
    if status>=0:
        q &= Q(status=status)
    if super_admin==0:
        q &= Q(add_uid=admin_id)

    user_objs = await customers_controller.categorys()
    total, data = await customers_controller.do_list(page=page, page_size=page_size, search=q)
    for item in data:
        item["status_name"] = settings.CUSTOMERS_STATUS[item["status"]]
        item["user_name"] = user_objs[item["add_uid"]]
    return SuccessExtra(data=data, total=total, page=page, page_size=page_size)

@customers_list_router.get("/get", summary=f"查看{model_name}")
async def do_get(
        data_id: int = Query(..., description="ID"),
):
    data = await customers_controller.do_detail(data_id=data_id)
    return Success(data=data)

@customers_list_router.post("/create", summary=f"创建{model_name}")
async def do_create(
        category_in: CustomersCreate,
):
    await customers_controller.do_create(obj_in=category_in)
    return Success(msg="创建成功")

@customers_list_router.post("/update", summary=f"更新{model_name}")
async def do_update(
        category_in: CustomersUpdate,
):
    if category_in.price > 0:
        category_in.status = 1
    await customers_controller.do_update(obj_in=category_in)
    return Success(msg="更新成功")

@customers_list_router.delete("/delete", summary=f"删除{model_name}")
async def do_delete(
        data_id: int = Query(..., description="ID"),
):
    await customers_controller.do_delete(data_id=data_id)
    return Success(msg="删除成功")


@customers_list_router.get("/status_level", summary="获取状态")
async def status_level():
    level_objs = settings.CUSTOMERS_STATUS
    data = []
    key = 0
    for level in level_objs:
        if level:
            data.append({"label":level,"value":key})
        key += 1
    return Success(data=data)

@customers_list_router.post("/file", summary="上传图片")
async def do_file(file: UploadFile):
    url = await upload_image(file,"customers")
    if not url:
        return Fail(code=400, msg="图片格式错误")
    data = {"url": url}
    return Success(data=data)


